# local_qa.py
import requests
import json

class LocalAIAssistant:
    def __init__(self):
        self.ollama_url = "http://192.168.0.103:11434"
    
    def ask_question(self, question, context=""):
        prompt = f"""
        基于以下上下文信息回答问题：
        
        上下文：{context}
        
        问题：{question}
        
        请提供准确、简洁的回答：
        """
        
        response = requests.post(
            f"{self.ollama_url}/api/generate",
            json={
                "model": "phi",
                "prompt": prompt,
                "stream": False,
                "options": {
                    "temperature": 0.1,
                    "num_predict": 256
                }
            }
        )
        result = response.json()
        print(result)
        return result["response"]

# 使用示例
assistant = LocalAIAssistant()
answer = assistant.ask_question("什么是机器学习？")
print(answer)